import { defineStore } from "pinia";
import type { reqCategoryReturnType } from "@/api/attr"
import {
    requestCategory1List,
    requestCategory2List,
    requestCategory3List,
} from "@/api/attr"
import { ElMessage } from "element-plus";



//定义state的类型
export interface attrStateType {
    category1List: reqCategoryReturnType;
    category2List: reqCategoryReturnType;
    category3List: reqCategoryReturnType;
    category1Id: number | null;
    category2Id: number | null;
    category3Id: number | null;
}
//创建商品属性相关的小仓库
const useAttrStore = defineStore("attr", {
    state: (): attrStateType => {
        return {
            category1List: [],
            category2List: [],
            category3List: [],
            category1Id: null,
            category2Id: null,
            category3Id: null,
        }
    },
    actions: {
        //1.请求一级列表
        async getCategory1List() {
            try {
                //发送一级列表请求
                const result = await requestCategory1List()
                //把请求回来的一级列表的数据放在小菠萝
                this.category1List = result
            } catch (error) {
                //请求失败提示
                ElMessage.error("请求一级列表失败")
            }
        },
        //2.请求二级列表
        async getCategory2List() {
            try {
                //发送二级列表请求
                const result = await requestCategory2List(this.category1Id as number)
                //把请求回来的二级列表的数据放在小菠萝
                this.category2List = result
            } catch (error) {
                //请求失败提示
                ElMessage.error("请求二级列表失败")
            }
        },
        //2.请求三级列表
        async getCategory3List() {
            try {
                //发送三级列表请求
                const result = await requestCategory3List(this.category2Id as number)
                //把请求回来的二级列表的数据放在小菠萝
                this.category3List = result
            } catch (error) {
                //请求失败提示
                ElMessage.error("请求三级列表失败")
            }
        },
    },
    getters: {},
})

export default useAttrStore